﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using iTunesExport.Parser;

namespace iTunesUtil
{    
    public partial class CombineCollectionsDlg : Form
    {
        static Dictionary<string, string> dict = new Dictionary<string, string>();
        TrackCollection collection;        
        long noTracks = 0;
        long sizeMB = 0;
        string type;

        public TrackCollection Collection
        {
            get { return collection; }
        }
        public string Name { get; set; }

        public CombineCollectionsDlg(IEnumerable<TrackCollection> tcs, string type)
        {
            InitializeComponent();
            this.type = type;
            Name = string.Format("Generic {0}", DateTime.Now.ToLongTimeString());
            label1.Text = Name;
            foreach (TrackCollection tc in tcs)
            {
                int index = checkedListBox1.Items.Add(new TrackCollectionWrapper(tc));
                if (dict.ContainsKey(tc.Name))
                    checkedListBox1.SetItemChecked(index, true);
            }
        }
        private void OnOK(object sender, EventArgs e)
        {
            collection = new TrackCollection(Name, type);
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                if (checkedListBox1.GetItemChecked(i))
                {
                    foreach (Track t in GetTrackCollection(i).Tracks)
                    {
                        collection.Add(t);
                    }
                }
            }
            collection.GetInfo();
        }        
        private TrackCollection GetTrackCollection(int index)
        {
            return ((TrackCollectionWrapper)checkedListBox1.Items[index]).TC;
        }                   
        private void CalcNoTracks()
        {
            sizeMB = noTracks = 0;            
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                if (checkedListBox1.GetItemChecked(i))
                {
                    TrackCollection tc = GetTrackCollection(i);
                    noTracks += tc.Count;
                    sizeMB += tc.Size;
                }
            }
            this.Text = string.Format("Selected {0} tracks {1} MB", noTracks, sizeMB);
        }
        private void OnSelectionChanged(object sender, EventArgs e)
        {
            int index = checkedListBox1.SelectedIndex;
            if (index == -1)
                return;

            TrackCollection tc = GetTrackCollection(index);
            tc.GetInfo();
            

            if (checkedListBox1.GetItemChecked(index))
            {             
                if (!dict.ContainsKey(tc.Name))
                    dict.Add(tc.Name, tc.Name);
            }
            else
            {                
                if (dict.ContainsKey(tc.Name))
                    dict.Remove(tc.Name);
            }
            CalcNoTracks();            
        }
        private void OnSelectAll(object sender, EventArgs e)
        {
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                checkedListBox1.SetItemChecked(i, true);
            }
            CalcNoTracks();
        }
        private void OnDeselectAll(object sender, EventArgs e)
        {
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                checkedListBox1.SetItemChecked(i, false);
            }
            CalcNoTracks();
        }
    }
    internal class TrackCollectionWrapper 
    {
        TrackCollection tc;
        public TrackCollectionWrapper(TrackCollection tc)
        {
            this.tc = tc;
        }

        public TrackCollection TC { get { return tc; } }
        public override string ToString()
        {
            return tc.ToStringDetailed(false);
        }
    }
}
